import request from "@/utils/request";
import type { AxiosPromise } from "axios";
import type { AuthApi, CaptchaResult, LoginData, LoginResult, UserInfo, RolesPerms } from "./types";
import { useUserStore } from "@/stores/modules/user";
import { vueMetaEnv } from "@/settings";

class AuthApiImpl implements AuthApi {
  selectDsdRoles(): AxiosPromise {
    return request({
      url: "/v1/rbac/auth/select_dsd_roles?tenant=" + useUserStore().user.tenant,
      method: "get"
    });
  }

  login(data: LoginData): AxiosPromise<LoginResult> {
    const formData = new FormData();
    formData.append("tenant", useUserStore().user.tenant);
    formData.append("directoryId", "jmash");
    formData.append("userName", data.username);
    formData.append("encodePwd", data.password);
    formData.append("scope", data.scope);
    formData.append("clientId", vueMetaEnv.APP_CLIENT_ID);
    formData.append("captchaId", data.captchaKey as string);
    formData.append("captchaCode", data.captchaCode as string);
    return request({
      url: "/v1/rbac/auth/login",
      method: "post",
      data: formData,
      headers: {
        Authorization: false
      }
    });
  }

  refreshToken(token: string): AxiosPromise<LoginResult> {
    const formData = new FormData();
    formData.append("tenant", useUserStore().user.tenant);
    formData.append("refreshToken", token);
    formData.append("clientId", vueMetaEnv.APP_CLIENT_ID);
    return request({
      url: "/v1/rbac/auth/refresh_token",
      method: "post",
      data: formData
    });
  }

  logout(accessToken: string): AxiosPromise {
    return request({
      url:
        "/v1/rbac/auth/logout?tenant=" +
        useUserStore().user.tenant +
        "&client_id=" +
        vueMetaEnv.APP_CLIENT_ID +
        "&accessToken=" +
        accessToken,
      method: "delete"
    });
  }

  captcha(): AxiosPromise<CaptchaResult> {
    return request({
      url: "/v1/captcha/classic",
      method: "get"
    });
  }

  userInfo(): AxiosPromise<UserInfo> {
    return request({
      url: "/v1/rbac/auth/user?tenant=" + useUserStore().user.tenant,
      method: "get"
    });
  }

  userRolesPerms(): AxiosPromise<RolesPerms> {
    return request({
      url: "/v1/rbac/auth/roles_perms?tenant=" + useUserStore().user.tenant,
      method: "get"
    });
  }

  updateUser(data: any): AxiosPromise {
    return request({
      url: "/v1/rbac/auth/user",
      method: "patch",
      data: data
    });
  }

  updatePwd(encodeOldPwd: string, encodeNewPwd: string): AxiosPromise {
    const data = {
      tenant: useUserStore().user.tenant,
      encodeOldPwd: encodeOldPwd,
      encodeNewPwd: encodeNewPwd
    };
    return request({
      url: "/v1/rbac/auth/change_pwd",
      method: "patch",
      data: data
    });
  }

  allowRunAsUser(): AxiosPromise {
    return request({
      url: "/v1/rbac/auth/allow_run_as_user?tenant=" + useUserStore().user.tenant,
      method: "get"
    });
  }

  runAsUser(userId: string): AxiosPromise {
    const data = {
      tenant: useUserStore().user.tenant,
      userId: userId
    };
    return request({
      url: "/v1/rbac/auth/run_as",
      method: "post",
      data: data
    });
  }

  sendMobileCode(data: any): AxiosPromise<CaptchaResult> {
    return request({
      url: "/v1/rbac/auth/sendcode",
      method: "post",
      data: data
    });
  }

  mobileCodeLogin(data: any): AxiosPromise<CaptchaResult> {
    return request({
      url: "/v1/rbac/auth/logincode",
      method: "post",
      data: data
    });
  }

  deptTree(deptId?: string) {
    const query = {
      tenant: useUserStore().user.tenant,
      excludeId: deptId
    };
    return request({
      url: "/v1/rbac/dept/treelist",
      method: "get",
      params: query
    });
  }

  jobTree() {
    const query = {
      tenant: useUserStore().user.tenant,
      roleType: "job",
      hasRoleType: true
    };
    return request({
      url: "/v1/rbac/role/treelist",
      method: "get",
      params: query
    });
  }
}

const authApi = new AuthApiImpl();

export { authApi };
